package com.anzia.list;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

/**
 *@ClassName LinkedList_Demo
 *@Description
 *@Author Anzia.
 *@Date 2021/4/10 11:59
 *@Version 1.0
 **/
public class LinkedList_Demo {
    public static void main(String[] args) {
        //创建集合
        LinkedList<Object> linkedList = new LinkedList<>();
        Student student1 = new Student("周星驰",20);
        Student student2 = new Student("刘德华",18);
        Student student3 = new Student("梁朝伟",19);

        //添加数据
        linkedList.add(student1);
        linkedList.add(student2);
        linkedList.add(student3);

        System.out.println("元素个数：" + linkedList.size());
        System.out.println(linkedList);

        //删除
        //linkedList.remove(new Student("周星驰",20));
        //linkedList.clear();
        //System.out.println(linkedList);

        //遍历
        //for遍历
        System.out.println("--------------");
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.println(linkedList.get(i));
        }
        //增强for
        System.out.println("--------------");
        for (Object o : linkedList) {
            Student s = (Student) o;
            System.out.println(s);
        }
        //迭代器
        System.out.println("--------------");
        Iterator<Object> iterator = linkedList.iterator();
        while (iterator.hasNext()) {
            Student student = (Student) iterator.next();
            System.out.println(student);
        }
        //列表迭代器
        System.out.println("--------------");
        ListIterator<Object> listIterator = linkedList.listIterator();
        while (listIterator.hasNext()) {
            System.out.println(listIterator.nextIndex() + " : " +listIterator.next());
        }
        System.out.println("--------------");
        while (listIterator.hasPrevious()) {
            System.out.println(listIterator.nextIndex() + " : " +listIterator.previous());
        }

        //判断
        System.out.println("--------------");
        System.out.println(linkedList.contains(student2));
        System.out.println(linkedList.isEmpty());

        //获取
        System.out.println(linkedList.indexOf(student1));
    }
}
